"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var http_1 = require("@angular/http");
var HttpService = /** @class */ (function () {
    function HttpService(http, jsonp, oauthService) {
        this.http = http;
        this.jsonp = jsonp;
        this.oauthService = oauthService;
        this.r = {
            protocol: /([^\/]+:)\/\/(.*)/i,
            host: /(^[^\:\/]+)((?:\/|:|$)?.*)/,
            port: /\:?([^\/]*)(\/?.*)/,
            pathname: /([^\?#]+)(\??[^#]*)(#?.*)/
        };
    }
    HttpService.prototype.parseUrl = function (url) {
        var tmp, res = {};
        res['href'] = url;
        for (var p in this.r) {
            tmp = this.r[p].exec(url);
            res[p] = tmp[1];
            url = tmp[2];
            if (url === '') {
                url = '/';
            }
            if (p === 'pathname') {
                res['pathname'] = tmp[1];
                res['search'] = tmp[2];
                res['hash'] = tmp[3];
            }
        }
        console.log(url);
        return res;
    };
    ;
    // get方法
    HttpService.prototype.httpGet = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        // const headers = new Headers();
        return this.http.get(url, options).map(function (res) { return res.json(); });
    };
    // get方法 oauth
    HttpService.prototype.httpGetAuth = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        var token = this.oauthService.getAccessToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token);
            // options.headers.append('x-xsrf-token',token);
        }
        var nowApplet = JSON.parse(localStorage.getItem('nowApplet'));
        if (nowApplet) {
            var userInfo = JSON.parse(localStorage.getItem('userInfo'));
            if (userInfo) {
                var applet_uuid = nowApplet.applet_uuid;
                var params = new http_1.URLSearchParams();
                params.set('applet_uuid', applet_uuid);
                options.params = params;
            }
        }
        return this.http.get(url, options).map(function (res) { return res.json(); });
    };
    // httpGetWithJWT(url: string, data:any) {
    //     let token = localStorage.getItem('token');
    //     let params = new URLSearchParams();
    //     for(let key in data) {
    //         params.set(key.toString(), data[key]);
    //     }
    //     //params.set('token',token.access_token);
    //
    //     return this.http.get(url, params).map(res => res.json());
    // }
    HttpService.prototype.httpPut = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        return this.http.put(url, options).map(function (res) { return res.json(); });
    };
    HttpService.prototype.httpPutAuth = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        var token = this.oauthService.getAccessToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token);
        }
        return this.http.put(url, options).map(function (res) { return res.json(); });
    };
    HttpService.prototype.httpDelete = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        return this.http.delete(url, options).map(function (res) { return res.json(); });
    };
    HttpService.prototype.httpDeleteAuth = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        var token = this.oauthService.getAccessToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token);
        }
        return this.http.delete(url, options).map(function (res) { return res.json(); });
    };
    // post 方法
    HttpService.prototype.httpPost = function (url, body, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        return this.http.post(url, body, options).map(function (res) { return res.json(); });
    };
    // post 方法 oauth
    HttpService.prototype.httpPostAuth = function (url, body, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        var token = this.oauthService.getAccessToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token);
        }
        var nowApplet = JSON.parse(localStorage.getItem('nowApplet'));
        if (nowApplet) {
            var userInfo = JSON.parse(localStorage.getItem('userInfo'));
            if (userInfo) {
                var applet_uuid = nowApplet.applet_uuid;
                var params = new http_1.URLSearchParams();
                params.set('applet_uuid', applet_uuid);
                options.params = params;
            }
        }
        return this.http.post(url, body, options).map(function (res) { return res.json(); });
    };
    HttpService.prototype.httpPostAuth1 = function (url, body, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        var token = this.oauthService.getAccessToken();
        if (token) {
            options.headers.append('Authorization', 'Bearer ' + token);
        }
        /*if (token) {
         options.headers.append('Authorization', 'Bearer ' + token.auth.original.access_token);
         }*/
        return this.http.post(url, body, options).map(function (res) { return res; });
    };
    // jsonp跨域请求
    HttpService.prototype.jsonpGet = function (url, options) {
        if (!options) {
            options = new http_1.RequestOptions({});
        }
        if (!options.headers) {
            this.setDefaultHeader(options);
        }
        return this.jsonp.get(url, options).map(function (res) { return res.json(); });
    };
    // 错误处理
    HttpService.prototype.handleError = function (error) {
        console.error(error);
        // return Observable.throw(error.json().error || 'Server error');
    };
    HttpService.prototype.setDefaultHeader = function (options) {
        options.headers = new http_1.Headers({
            // 'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json',
        });
    };
    HttpService.prototype.catchAuthError = function (self) {
        /*return (res: Response) => {
         console.log(res);
         if (res.status === 401 || res.status === 403) {
         // if not authenticated
         console.log(res);
         }
         };*/
    };
    HttpService = __decorate([
        core_1.Injectable()
    ], HttpService);
    return HttpService;
}());
exports.HttpService = HttpService;
